/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.bs.analisis.configuration;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.bs.analisis.execution.exception.ConfigurationException;

/**
 *
 * @author gbran
 */
public class ConfigurationService<T> {

    private String configurationPath;
    private Class<T> configurationResource;
    private JAXBContext context;

    public ConfigurationService(String configurationPath, Class<T> configurationResource) {
        this.configurationPath = configurationPath;
        this.configurationResource = configurationResource;
    }

    public ConfigurationService init() throws ConfigurationException {
        try {
            this.context = JAXBContext.newInstance(configurationResource);
            return this;
        } catch (JAXBException e) {
            throw new ConfigurationException(e.getMessage());
        }
    }

    public void save(T configuration) throws ConfigurationException {
        OutputStream outputStream = null;
        try {
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_ENCODING, "UTF-8");
            marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            outputStream = new FileOutputStream(configurationPath);
            marshaller.marshal(configuration, outputStream);
        } catch (JAXBException e) {
            throw new ConfigurationException(e.getMessage());
        } catch (FileNotFoundException e) {
            throw new ConfigurationException(e.getMessage());
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    throw new ConfigurationException(e.getMessage());
                }
            }
        }
    }

    public T load() throws ConfigurationException {
        try {
            File file = new File(configurationPath);
            JAXBContext jaxbContext = JAXBContext.newInstance(configurationResource);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            return (T) jaxbUnmarshaller.unmarshal(file);
        } catch (JAXBException e) {
            throw new ConfigurationException(e.getMessage());
        }
    }

    public String getConfigurationPath() {
        return configurationPath;
    }

    public void setConfigurationPath(String configurationPath) {
        this.configurationPath = configurationPath;
    }

    public Class<T> getConfigurationResource() {
        return configurationResource;
    }

    public void setConfigurationResource(Class<T> configurationResource) {
        this.configurationResource = configurationResource;
    }
}
